layui.use(['layer','table','form','laydate','element'],function () {
   var layer = layui.layer;
   var table = layui.table;
   var element = layui.element;
   var form = layui.form;
   var laydate = layui.laydate;

    laydate.render({
        elem: '#RWRQ',
        type: 'date',
        trigger: 'click',
        format: 'yyyy-MM-dd'
    });
    laydate.render({
        elem: '#CSRQ',
        type: 'date',
        trigger: 'click',
        format: 'yyyy-MM-dd'
    });

   //获取车辆信息：表格
   var table1Info = table.render({
        elem: '#table1',
        url: '/car/getCarInfo',
        method: 'post',
        cols: [
            [
                // {field:'id',title:'ID',align:'center',width:'5%',sort:true,templet: function (d) { return d.LAY_TABLE_INDEX+1}},
                {field: 'id', title: 'ID', align: 'center', width: '5%', type: 'numbers'},
                {field: 'CBH', title: '车辆编号', align: 'center', width: '10%'},
                {field: 'CHM', title: '车牌号码', align: 'center', width: '8%'},
                {field: 'CLX', title: '车辆类型', align: 'center', width: '10%'},
                {field: 'CZZ', title: '车辆载重(kg)', align: 'center', width: '10%'},
                {field: 'RWMC', title: '任务名称', align: 'center', width: '10%'},
                {field: 'RWRQ', title: '送货时间', align: 'center', width: '10%'},
                {field: 'SHDD', title: '送货地点', align: 'center', width: '8%'},
                {field: 'HWMC', title: '货物名称', align: 'center', width: '8%'},
                {field: 'BZ', title: '备注', align: 'center', width: '9%'},
                {field: 'ZT', title: '状态', align: 'center', width: '5%'},
                {field: 'action1', title: '操作', align: 'center', toolbar: '#bar1', width: '7%'}
            ]
        ],
        page: true

    });
    //编辑,删除表格行数据
    table.on('tool(table1)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent == "edit") {
            layer.open({
                type: 2,
                title: '修改车辆信息',
                shadeClose: false,
                shade: 0.8,
                maxmin: true,
                area: ['60%', '85%'],
                content: '/car/editCarInfo?id=' + data.id
            });
        }
        if (layEvent == "del") {
            $.ajax({
                type:'post',
                url:'/car/delCarInfo',
                data:{
                    "id":data.id
                },
                success:function (result) {
                    if (result=="success"){
                        layer.msg("删除成功！",{icon:1,time:1000},function (index) {
                            layer.close(index);
                            location.reload();
                        });
                    }
                    if (result=="fail"){
                        layer.msg("删除失败！",{icon:2,time:1000},function (index) {
                            layer.close(index);
                        });
                    }
                    if (result=="infoError"){
                        layer.msg("删除失败！",{icon:2,time:1000},function (index) {
                            layer.close(index);
                        });
                    }
                },
                error:function (error) {
                    layer.msg("404");
                }
            });
        }
    });
    form.on('submit(editCar)', function (data) {
        data.field.BZ=$("#BZ").val();
        $.ajax({
            type:'post',
            url:'/car/editCar',
            data:JSON.stringify(data.field),
            contentType:'application/json; charset=utf-8',
            success:function (result) {
                if (result=='success'){
                    layer.msg('修改成功,3s后自动关闭',{icon:1},function () {
                        parent.location.reload();
                    });
                }else {
                    layer.msg('修改失败,3s后自动关闭',{icon:1});
                }
            },
            error:function (error) {
                layer.msg('404,error',{icon:2});
            }
        });
        return false;
    });
    form.on('submit(add)',function (data) {
        data.field.BZ=$("#BZ").val();
        console.log(data.field);
        $.ajax({
            type:'post',
            url:'/car/addCar',
            data:JSON.stringify(data.field),
            contentType:'application/json; charset=utf-8',
            success:function (result) {
                if (result=='success'){
                    layer.msg('添加成功,3s后自动关闭',{icon:1});
                }else {
                    layer.msg('添加失败,3s后自动关闭',{icon:2});
                }
            },
            error:function (error) {
                layer.msg('404,error',{icon:2});
            }
        });
        return false;
    });

    form.on('submit(searchcar)',function (data) {
        console.log(data.field.carNum);
        var carNum = data.field.carNum;
        table1Info = table.render({
            elem: '#table1',
            url: '/car/getCarInfo?carNum='+carNum,
            method: 'post',
            cols: [
                [
                    // {field:'id',title:'ID',align:'center',width:'5%',sort:true,templet: function (d) { return d.LAY_TABLE_INDEX+1}},
                    {field: 'id', title: 'ID', align: 'center', width: '5%', type: 'numbers'},
                    {field: 'CBH', title: '车辆编号', align: 'center', width: '10%'},
                    {field: 'CHM', title: '车牌号码', align: 'center', width: '8%'},
                    {field: 'CLX', title: '车辆类型', align: 'center', width: '10%'},
                    {field: 'CZZ', title: '车辆载重(kg)', align: 'center', width: '10%'},
                    {field: 'RWMC', title: '任务名称', align: 'center', width: '10%'},
                    {field: 'RWRQ', title: '送货时间', align: 'center', width: '10%'},
                    {field: 'SHDD', title: '送货地点', align: 'center', width: '8%'},
                    {field: 'HWMC', title: '货物名称', align: 'center', width: '8%'},
                    {field: 'BZ', title: '备注', align: 'center', width: '9%'},
                    {field: 'ZT', title: '状态', align: 'center', width: '5%'},
                    {field: 'action1', title: '操作', align: 'center', toolbar: '#bar1', width: '7%'}
                ]
            ],
            page: true

        });
       return false;
    });

    /*
        司机页
     */
    var table2Info = table.render({
        elem: '#table2',
        url: '/car/getDriverInfo',
        method: 'post',
        cols: [
            [
                // {field:'id',title:'ID',align:'center',width:'5%',sort:true,templet: function (d) { return d.LAY_TABLE_INDEX+1}},
                // {field: 'id', title: 'ID', align: 'center', width: '5%', type: 'numbers'},
                {field: 'XM', title: '姓名', align: 'center', width: '8%'},
                {field: 'XB', title: '性别', align: 'center', width: '5%'},
                {field: 'NL', title: '年龄', align: 'center', width: '5%'},
                {field: 'CSRQ', title: '出生日期', align: 'center', width: '7%'},
                {field: 'SJH', title: '手机号', align: 'center', width: '10%'},
                {field: 'SFZ', title: '身份证', align: 'center', width: '12%'},
                {field: 'CPHM', title: '车牌号', align: 'center', width: '8%'},
                {field: 'JSZLX', title: '驾证类型', align: 'center', width: '8%'},
                {field: 'JSZBH', title: '驾证编号', align: 'center', width: '10%'},
                {field: 'DZ', title: '家庭地址', align: 'center', width: '12%'},
                {field: 'ZT', title: '状态', align: 'center', width: '8%'},
                {field: 'action1', title: '操作', align: 'center', toolbar: '#bar1', width: '7%'}
            ]
        ],
        page: true
    });
    form.on('submit(sjsubmit)',function (data) {
        var drivertip = data.field.drivertip;
        table2Info = table.render({
            elem: '#table2',
            url: '/car/getDriverInfo?drivertip='+drivertip,
            method: 'post',
            cols: [
                [
                    // {field:'id',title:'ID',align:'center',width:'5%',sort:true,templet: function (d) { return d.LAY_TABLE_INDEX+1}},
                    // {field: 'id', title: 'ID', align: 'center', width: '5%', type: 'numbers'},
                    {field: 'XM', title: '姓名', align: 'center', width: '8%'},
                    {field: 'XB', title: '性别', align: 'center', width: '5%'},
                    {field: 'NL', title: '年龄', align: 'center', width: '5%'},
                    {field: 'CSRQ', title: '出生日期', align: 'center', width: '7%'},
                    {field: 'SJH', title: '手机号', align: 'center', width: '10%'},
                    {field: 'SFZ', title: '身份证', align: 'center', width: '12%'},
                    {field: 'CPHM', title: '车牌号', align: 'center', width: '8%'},
                    {field: 'JSZLX', title: '驾证类型', align: 'center', width: '8%'},
                    {field: 'JSZBH', title: '驾证编号', align: 'center', width: '10%'},
                    {field: 'DZ', title: '家庭地址', align: 'center', width: '12%'},
                    {field: 'ZT', title: '状态', align: 'center', width: '8%'},
                    {field: 'action1', title: '操作', align: 'center', toolbar: '#bar1', width: '7%'}
                ]
            ],
            page: true
        });
    });
    form.on('submit(addDriver)',function (data) {
        console.log(data.field);
        $.ajax({
            type:'post',
            url:'/car/addDriver',
            data:JSON.stringify(data.field),
            contentType:'application/json; charset=utf-8',
            success:function (result) {
                if (result=='success'){
                    layer.msg('添加成功,3s后自动关闭',{icon:1},function () {
                    });
                }else {
                    layer.msg('添加失败,3s后自动关闭',{icon:1});
                }
            },
            error:function (error) {
                layer.msg('404,error',{icon:2});
            }
        });
        return false;
    });
    form.on('submit(addD)',function (data) {
        console.log(data.field);
        $.ajax({
            type:'post',
            url:'/car/editDriver',
            data:JSON.stringify(data.field),
            contentType:'application/json; charset=utf-8',
            success:function (result) {
                if (result=='success'){
                    layer.msg('修改成功,3s后自动关闭',{icon:1},function () {
                        parent.location.reload();
                    });
                }else {
                    layer.msg('修改失败,3s后自动关闭',{icon:1});
                }
            },
            error:function (error) {
                layer.msg('404,error',{icon:2});
            }
        });
        return false;
    });
    //编辑,删除表格行数据
    table.on('tool(table2)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent == "edit") {
            layer.open({
                type: 2,
                title: '修改司机信息',
                shadeClose: false,
                shade: 0.8,
                maxmin: true,
                area: ['60%', '85%'],
                content: '/car/editDriverInfo?id=' + data.id
            });
        }
        if (layEvent == "del") {
            $.ajax({
                type:'post',
                url:'/car/delDriverInfo',
                data:{
                    "id":data.id
                },
                success:function (result) {
                    if (result=="success"){
                        layer.msg("删除成功！",{icon:1,time:1000},function (index) {
                            layer.close(index);
                            location.reload();
                        });
                    }
                    if (result=="fail"){
                        layer.msg("删除失败！",{icon:2,time:1000},function (index) {
                            layer.close(index);
                        });
                    }
                    if (result=="infoError"){
                        layer.msg("删除失败！",{icon:2,time:1000},function (index) {
                            layer.close(index);
                        });
                    }
                },
                error:function (error) {
                    layer.msg("404");
                }
            });
        }
    });
});

$(function () {
    $("#addCarInfo").click(function () {
        location.href='/car/addCarInfo';
    });
});
$(function () {
    $("#addDriverInfo").click(function () {
        location.href='/car/addDriverInfo';
    });
});